import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RoundInfo, RecmInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { Exception } from "sass";

export interface IDynamicArrayOption {
    betArray: number[];
    addStep: number;
}

export class DynamicArrayMoneyStrategy implements IMoneyStrategy {

    strategyName: string = "DynamicArrayMoneyStrategy";
    debugCompoenent: string = "DynamicArrayMoneyStrategyDebug";

    betArray: number[] = [];
    addStep: number = 10;
    option: IDynamicArrayOption;
    recmV: EnumBjlResult = EnumBjlResult.U;
    recmMoney: number = 0;

    constructor(name: string, option?: IDynamicArrayOption) {
        this.strategyName = name;
        if (!option) {
            option = {
                betArray: [50, 50, 50],
                addStep: 10,
            }
        }
        this.option = option;
        this.resetStrategy();
    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = roundInfo.userRecm;
        let recmMoney = 0;
        let currentMoney = this.recmMoney;

        if (recmInfo.roundSummaryInfo.waterMoney >= 10) {
            // let min = this.betArray[0];
            // this.betArray.push(min + 10);
            this.betArray[0] += 10;
            recmInfo.roundSummaryInfo.waterMoney -= 10;
        }

        if (roundInfo.recmResult == EnumBetResult.Success) {
            let idx = this.betArray.findIndex(x => x == currentMoney);
            if (idx >= 0) {
                this.betArray.splice(idx, 1);
            }

            if (this.betArray.length == 0) throw Error("已通关");

            this.betArray = this.betArray.sort((a, b) => a - b);
            //recmMoney = this.betArray[this.betArray.length - 1];

        }
        else if (roundInfo.recmResult == EnumBetResult.Fail) {
            currentMoney = currentMoney + this.addStep;
            if (currentMoney >= 300) {
                let aggretAmount = parseInt(Math.round(currentMoney / this.betArray.length).toString());
                this.betArray.forEach(it => it += aggretAmount);
            }
            this.betArray.push(currentMoney);
            this.betArray = this.betArray.sort((a, b) => a - b);
            //recmMoney = this.betArray[0];
        }
        else {
            this.betArray = this.betArray.sort((a, b) => a - b);
            recmMoney = this.betArray[0];
        }

        let ridx = parseInt((Math.random() * 1000).toString()) % this.betArray.length;
        recmMoney = this.betArray[ridx];

        this.recmV = recmV;
        this.recmMoney = recmMoney;
        return [this.recmV, this.recmMoney]
    }
    showMoney(roundInfo?: RoundInfo): [EnumBjlResult, number] {
        return [this.recmV, this.recmMoney];
    }
    resetStrategy(): void {
        this.betArray = this.option.betArray;
        this.recmV = EnumBjlResult.U;
        this.recmMoney = 0;
        this.addStep = this.option.addStep;
    }
    resetRecm(): void {
        this.recmV = EnumBjlResult.U;
        this.recmMoney = 0;
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as DynamicArrayMoneyStrategy;
        if (obj) {
            this.betArray = obj.betArray;
            this.addStep = obj.addStep;
            this.recmV = obj.recmV;
            this.recmMoney = obj.recmMoney;
        }
    }

}